//
//  ECMineFavCell.m
//  Edu_iOS2018
//
//  Created by apple on 2018/1/17.
//  Copyright © 2018年 yanyan. All rights reserved.
//

#import "ECMineFavCell.h"
#import "ECMineFavMode.h"

@interface ECMineFavCell(){
    ///课程图片
    UIImageView *iconImageView;
    ///小表image
    UIImageView *littleTimeImage;
    ///课程名称lab
    UILabel *titleLab;
    ///学习人数lab
    UILabel *countLab;
    ///价格lab
    UILabel *priceLab;
    ///有效天数lab
    UILabel *dayOfNumLab;
    
}

@end

@implementation ECMineFavCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = 0;
        
        [self createCellUI];
    }
    return self;
}

-(void)createCellUI{
    iconImageView = [[UIImageView alloc]init];
    [self.contentView addSubview:iconImageView];
    iconImageView.layer.masksToBounds = YES;
    iconImageView.layer.cornerRadius = 3.f;
    
    littleTimeImage = [[UIImageView alloc]init];
    [self.contentView addSubview:littleTimeImage];
    littleTimeImage.image = [UIImage imageNamed:@"Mine_littleTime"];
    
    titleLab  = [[UILabel alloc]init];
    [self.contentView addSubview:titleLab];
    titleLab.font = [UIFont systemFontOfSize:14*kPercentage];
    
    countLab  = [[UILabel alloc]init];
    [self.contentView addSubview:countLab];
    countLab.textColor = [UIColor colorWithHexString:@"#7A7A7A"];
    countLab.font = [UIFont systemFontOfSize:12*kPercentage];
    
    priceLab  = [[UILabel alloc]init];
    [self.contentView addSubview:priceLab];
    priceLab.font = [UIFont systemFontOfSize:13*kPercentage];

    dayOfNumLab  = [[UILabel alloc]init];
    [self.contentView addSubview:dayOfNumLab];
    dayOfNumLab.textAlignment = NSTextAlignmentRight;
    dayOfNumLab.font = [UIFont systemFontOfSize:13*kPercentage];
    
    UIView *lineView = [[UILabel alloc]init];
    [self.contentView addSubview:lineView];
    lineView.backgroundColor = [UIColor colorWithHexString:@"#E5E5E5"];
    
    WeakSelf(weakSelf);
    [iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.contentView.mas_top).offset(kProportion_Height(48));
        make.left.equalTo(weakSelf.contentView.mas_left).offset(kProportion_Width(45));
        make.width.equalTo(@(kProportion_Width(324)));
        make.bottom.equalTo(weakSelf.contentView.mas_bottom).offset(-kProportion_Height(48));
    }];
    
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(iconImageView.mas_right).offset(kProportion_Width(36));
        make.top.equalTo(iconImageView.mas_top);
        make.right.equalTo(weakSelf.contentView.mas_right).offset(-kProportion_Width(45));
        make.height.equalTo(@(kProportion_Height(46)));
    }];
    
    [countLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLab.mas_left);
        make.top.equalTo(titleLab.mas_bottom).offset(kProportion_Height(15));
        make.bottom.equalTo(priceLab.mas_top).offset(-kProportion_Height(15));
        make.width.equalTo(@150);
    }];
    
    [priceLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(iconImageView.mas_bottom);
        make.left.equalTo(titleLab.mas_left);
        make.height.equalTo(@(kProportion_Height(36)));
        make.width.equalTo(countLab.mas_width);
    }];
    
    [dayOfNumLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.contentView.mas_right).offset(-kProportion_Width(45));
        make.centerY.equalTo(priceLab.mas_centerY);
        make.height.equalTo(priceLab.mas_height);
        make.width.equalTo(@(kProportion_Width(140)));
    }];
    
    [littleTimeImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(dayOfNumLab.mas_left).offset(0);
        make.centerY.equalTo(priceLab.mas_centerY);
        make.width.and.height.equalTo(@(kProportion_Width(36)));
    }];
    
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(iconImageView.mas_left);
        make.right.equalTo(dayOfNumLab.mas_right);
        make.bottom.equalTo(weakSelf.contentView.mas_bottom);
        make.height.equalTo(@1);
    }];
}

-(void)setContentMode:(ECMineFavMode *)contentMode{
    [iconImageView sd_setImageWithURL:[NSURL URLWithString:contentMode.picture] placeholderImage:[UIImage imageNamed:kEduCourseImagePlaceholderName]];
    titleLab.text = contentMode.title;
    countLab.text = [NSString stringWithFormat:@"%@人在学",contentMode.num];
    
    if ([contentMode.price isEqualToString:@"0.00"]) {
        priceLab.text = @"免费";
        priceLab.textColor = [UIColor colorWithHexString:@"279af7"];
    }else{
        priceLab.text = [NSString stringWithFormat:@"¥%@",contentMode.price];
        priceLab.textColor = [UIColor colorWithHexString:@"#EF2C00"];
    }
    
    if ([contentMode.expiryDay isEqualToString:@"0"]) {
        dayOfNumLab.text = @"永久";
    }else{
        dayOfNumLab.text = [NSString stringWithFormat:@"%@天",contentMode.expiryDay];
    }
}


-(void)layoutSubviews{
    
    for (UIControl *control in self.subviews){
        if ([control isMemberOfClass:NSClassFromString(@"UITableViewCellEditControl")]){
            for (UIView *view in control.subviews)
            {
                if ([view isKindOfClass: [UIImageView class]]) {
                    UIImageView *image=(UIImageView *)view;
                    if (self.selected) {
                        image.image=[UIImage imageNamed:@"Mine_circleImage_select"];
                    }
                    else
                    {
                        image.image=[UIImage imageNamed:@"Mine_circleImage_normal"];
                    }
                }
            }
        }
    }
    
    [super layoutSubviews];
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    for (UIControl *control in self.subviews){
        if ([control isMemberOfClass:NSClassFromString(@"UITableViewCellEditControl")]){
            for (UIView *view in control.subviews)
            {
                if ([view isKindOfClass: [UIImageView class]]) {
                    UIImageView *image=(UIImageView *)view;
                    if (!self.selected) {
                        image.image=[UIImage imageNamed:@"Mine_circleImage_normal"];
                    }
                }
            }
        }
    }
    
}


@end
